﻿--[[
   @brief    web_bottom logic for  web_browser bottom game state
   @author   harleyxwang
   @version  1.0
   @date     2016-01-30
--]]

web_bottom_control =
{
    init = function()
        base_utility.log("web_bottom_control.init called");
        ierd_tgp.tpf_ui.Require("web_bottom\\model.lua");
        ierd_tgp.tpf_ui.Require('game_library\\control.lua');

    end;

    on_login_success = function(event_id, json_param)
        --登录成功，设置uin
        local table_param = base_utility.json_to_table(json_param);
        web_bottom_model.uin = table_param.uin;
        ierd_tgp.tpf_ui.Log("web_bottom: on_login_success:uin="..web_bottom_model.uin);
    end; 

    update_game_state = function(event_id, json_param)
        local const_table_param = base_utility.json_to_table(json_param);
        if const_table_param.game_id == nil or 0 == const_table_param.game_id 
            or banner_model.GAME_CENTER_GAME_ID == const_table_param.game_id then
            return
        end
        
        -- 如果不是当前模块，则直接返回
        local module_id = banner_view.get_current_select_module();
        if module_id ~= const_table_param.game_id then return end

        local gl_state = banner_model.get_game_state(module_id);
        if gl_state == nil then return end

        local tbl_data = web_bottom_control.get_game_and_plugin_state_tbl(module_id);
        if true == web_bottom_control.table_is_empty(tbl_data) then return end

        local game_and_plugin_json = base_utility.table_to_json(tbl_data);
        web_utility.lua_call_js_ex("tgp_update_game_state", game_and_plugin_json,
            CID_MAINFRAME_QBWEB);
    end;

    update_plugin_state = function(event_id, json_str)
        ierd_tgp.tpf_ui.Log("[web_bottom_control]update_plugin_state,json_str="..json_str);
        local params = base_utility.json_to_table(json_str);
        if params.game_id == nil or 0 == params.game_id or
            banner_model.GAME_CENTER_GAME_ID == params.game_id then
            return
        end

        -- 如果不是当前模块，则直接返回
        local module_id = banner_view.get_current_select_module();
        if module_id ~= params.game_id then return end

        local plugin_state_info = banner_model.get_plugin_state(module_id);
        if plugin_state_info == nil then return end

        local tbl_data = web_bottom_control.get_game_and_plugin_state_tbl(module_id);
        if true == web_bottom_control.table_is_empty(tbl_data) then return end

        local game_and_plugin_json = base_utility.table_to_json(tbl_data);
        web_utility.lua_call_js_ex("tgp_update_plugin_state", game_and_plugin_json,
            CID_MAINFRAME_QBWEB);
    end;

    update_game_servers = function(event_id, json_str)
        local params = base_utility.json_to_table(json_str);
        if (params == nil) then return end
        local game_id = banner_view.get_current_select_module();
        if nil == game_id or banner_model.GAME_CENTER_GAME_ID == game_id then
            return
        end
        -- "0" 表示所有的游戏
        if 0 == params.game_id or game_id == params.game_id then
            ierd_tgp.tpf_ui.Log("[web_bottom_control]update_game_servers, game_id="..game_id);
            web_bottom_control.send_game_servers_info(game_id, 'tgp_update_server_state');
        end            
    end;

    send_game_servers_info = function(game_id, call_js_fun_name)
        -- 如果不是当前模块，则直接返回
        local module_id = banner_view.get_current_select_module();
        if module_id ~= game_id then return end

        ierd_tgp.tpf_ui.Log("[web_bottom_control]send_game_servers_info,game_id="..game_id);

        local base_info = banner_model.get_game_base_info(game_id);
        if base_info == nil then  
            ierd_tgp.tpf_ui.Log("[web_bottom_control]send_game_servers_info, base_info is nil.");     
            return;
        end;

        local game_servers_table = {};
        game_servers_table.game_type = base_info.type;
        game_servers_table.support_zone_choose = base_info.support_zone_choose;
        game_servers_table.game_id = game_id;

        local game_servers = banner_model.get_game_servers(game_id);
        if game_servers ~= nil then
            game_servers_table.server_count = game_servers.server_count;
            game_servers_table.selleted = game_servers.selleted;
            game_servers_table.net_mode = game_servers.net_mode;
        else
            game_servers_table.server_count = 0;
            game_servers_table.selleted = 0;
            game_servers_table.net_mode = 0;
        end
        --[[ lcu remove
        if game_servers_table ~= nil 
            and game_servers_table.game_id == 26 
            and game_servers_table.selleted ~= 0
            and web_bottom_model.cur_lol_server_id ~= game_servers_table.selleted then
            local game_lib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then 
                web_bottom_model.cur_lol_server_id = game_servers_table.selleted;
                game_lib:UpdateLcuServerSelected(game_servers_table.game_id, game_servers_table.selleted);
                game_library_control.update_lcu_state(nil,"");
            end
        end
        ]]

        game_servers_table.servers = {};
        local count = game_servers_table.server_count;
        local acce_module =
            base_utility.get_component_adapter("IAccelerate_module",
                "ierd_tgp.accelerate_module.GetAcceModule();");
        while (count > 0) do
            local real_ping_val = -1;
            if acce_module ~= nil then
                real_ping_val = acce_module:get_real_ping(game_id, game_servers.selleted);
            end;

            table.insert(game_servers_table.servers, {
                id = game_servers.servers[count - 1].id;
                name = game_servers.servers[count - 1].name;
                need_update = game_servers.servers[count - 1].need_update;
                pre_update = game_servers.servers[count - 1].pre_update;
                state = game_servers.servers[count - 1].state;
                acce_info = {
                    valid = game_servers.servers[count - 1].acce_info.valid;
                    global_acce_enabled = game_servers.servers[count - 1].acce_info.global_acce_enabled;
                    acce_mode = game_servers.servers[count - 1].acce_info.acce_mode;
                    acce_state = game_servers.servers[count - 1].acce_info.acce_state;
                    decision_code = game_servers.servers[count - 1].acce_info.decision_code;
                    direct_delay = game_servers.servers[count - 1].acce_info.direct_delay;
                    direct_lost = game_servers.servers[count - 1].acce_info.direct_lost;
                    proxy_delay = game_servers.servers[count - 1].acce_info.proxy_delay;
                    proxy_lost = game_servers.servers[count - 1].acce_info.proxy_lost;
                    delay_down = game_servers.servers[count - 1].acce_info.delay_down;
                    lost_down = game_servers.servers[count - 1].acce_info.lost_down;
                    direct_delay_raw = game_servers.servers[count - 1].acce_info.direct_delay_raw;
                    proxy_delay_raw_t1 = game_servers.servers[count - 1].acce_info.proxy_delay_raw_t1;
                    proxy_delay_raw_t2 = game_servers.servers[count - 1].acce_info.proxy_delay_raw_t2;
                    real_ping = real_ping_val;
                };
             });
            count = count - 1;
        end;
        local game_servers_json = base_utility.table_to_json(game_servers_table);
        if call_js_fun_name ~= nil and call_js_fun_name ~= "" then
            web_utility.lua_call_js_ex(call_js_fun_name, game_servers_json, CID_MAINFRAME_QBWEB);
        end
        ierd_tgp.tpf_ui.Log("[web_bottom_control]send_game_servers_info,game_servers_json="
            ..game_servers_json);        
    end;
    -- 给页面返回游戏和插件的状态信息
    get_game_and_plugin_state = function(event_id, json_str)
        local params = base_utility.json_to_table(json_str);
        local module_id = nil;
        if params ~= nil and params.callback_func ~= nil and params.callback_func ~= "" then
            if params.module_id ~= nil then
                module_id = tonumber(params.module_id);
            end

            local tbl_data = web_bottom_control.get_game_and_plugin_state_tbl(module_id);
            if true == web_bottom_control.table_is_empty(tbl_data) then return end
           
            local game_and_plugin_json = base_utility.table_to_json(tbl_data);
            web_utility.lua_call_js_ex(params.callback_func, game_and_plugin_json, CID_MAINFRAME_QBWEB);
        end
    end;
    -- 获取游戏状态的优先级
    get_game_state_priority = function(state_value, game_id)
        local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil == game_lib then
            return;
        end

        local channel = game_lib:get_game_distribution_channels(game_id);

        if banner_model.is_netbar == 1 and 
            banner_model.distribution_channels.CHANNELS_RAIL ~= channel and
            state_value == ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED then
            state_value = ierd_tgp.game_library.GAME_STATE_NORMAL;
        end
        return  banner_model.get_gamestate_priority(state_value);

    end;
    -- 判断是否提示联系网管
    need_contact_netbar_manager = function(game_id)
        if game_id == nil then
            return false;
        end
        if banner_model.is_netbar ~= 1 then
            return false;
        end
        local base_info = banner_model.get_game_base_info(game_id);
        if base_info == nil then
            return false;
        end
        if base_info.main_type == web_bottom_model.GAME_LIST_MAIN_TYPE_PHONE then
            -- 手游不需要联系网管
            return false;
        end
        local game_state = banner_model.get_game_state(game_id);
        if game_state.iState == ierd_tgp.game_library.GAME_STATE_NORMAL or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_RUNNING then
           return false;
        end
        local param = {
            first_line = LoadStr(ID_NO_GAME_INSTALL_TIP),
            icon = "ONEIMAGE:bottom_bar\\box.png?155,11,205,61;Center;0;",
        }
        if base_info.banner_icon_url ~= nil then
            param.icon = "ONEIMAGE:bottom_bar\\box.png?155,11,205,61|"..base_info.banner_icon_url..";Stretch;0";
        end
        event_center.send_event(sub_account_login_event.SUB_ACCOUNT_SHOW_TIPS, 
            base_utility.table_to_json(param));
        return true;
    end;
    -- js 回调函数
    on_js_callback = function(event_id, json_param)
        local param = base_utility.json_to_table(json_param);
        local func_name = param.func_name;
        local json = param.json;
        ierd_tgp.tpf_ui.Log("on_js_callback.func_name="..func_name);
        if func_name == "jc_get_server_state" then
            web_bottom_control.get_server_state(json);
        elseif func_name == "jc_open_game_path" then
            web_bottom_control.open_game_dir(json);
        elseif func_name == "jc_open_post" then
            web_bottom_control.open_post(json);
        elseif func_name == "jc_check_update" then
            web_bottom_control.check_update(json);
            -- web_bottom_control.send_bottom_bar_show_state_to_js(json);
        elseif func_name == "jc_start_game" then
            local lua_table = base_utility.json_to_table(json);
            if lua_table ~= nil then
                if web_bottom_control.need_contact_netbar_manager(lua_table.game_id) == false then
                    web_bottom_control.launch_game(lua_table.game_id, lua_table.server_id);  
                end
            end
        --[[ lcu remove
        elseif func_name == "jc_switch_game" then
            local lua_table = base_utility.json_to_table(json);
            if lua_table ~= nil then
                local game_lib = base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
                if game_lib ~= nil then
                    local lcu_state = ierd_tgp.game_library.LCUState:new();
                    game_lib:GetLCUState(lcu_state);
                    if lua_table.game_id == 26 and lcu_state.lcu_available == true then
                            game_lib:SetUsingLCU(not lua_table.lcu);
                    end
                    if lua_table.game_id == 26 
                        and lcu_state.lcu_available == false 
                        and lcu_state.is_lcu_server_launch == true then
                        ver_fallback_cfg = 1; -- 灰度大区使用默认LCU客户端
                        if lua_table.lcu == true then -- 切换成air
                            ver_fallback_cfg = 0
                        end
                        game_lib:SetAirFallbackCfg(ver_fallback_cfg);
                    end
                end
            end
        ]]
        elseif func_name == "jc_install_game" then
            local lua_table = base_utility.json_to_table(json);
            if lua_table ~= nil then
                if web_bottom_control.need_contact_netbar_manager(lua_table.game_id) == false then
                    web_bottom_control.install_game(lua_table.game_id);
                end
            end
        elseif func_name == "jc_update_game" then
            local lua_table = base_utility.json_to_table(json);
            if lua_table ~= nil then
                web_bottom_control.update_game(lua_table.game_id, lua_table.server_id);
            end
        elseif func_name == "jc_update_plugin" then
            local lua_table = base_utility.json_to_table(json);
            if lua_table ~= nil then
                web_bottom_control.update_plugin(lua_table.game_id);
            end
        else
            web_bottom_control.handle_js_game_action(func_name, json);
        end;
    end;
    -- 给页面返回游戏服务器状态
    get_server_state = function(json_str)
        local params = base_utility.json_to_table(json_str);
        local module_id = nil;
        if params ~= nil and params.callback_func ~= nil and params.callback_func ~= "" then
            if params.module_id ~= nil then
                module_id = tonumber(params.module_id);
            end

            if module_id ~= nil and module_id ~= 0 and 
                banner_model.GAME_CENTER_GAME_ID ~= module_id then
                ierd_tgp.tpf_ui.Log("[web_bottom_control]get_server_state, game_id="..module_id);        
                web_bottom_control.send_game_servers_info(module_id, params.callback_func);
            end
        end
    end;
    -- 启动游戏
    launch_game = function(game_id, server_id)
        ierd_tgp.tpf_ui.Log("[web_bottom_control]launch_game, game_id="..game_id..
                            ",server_id="..server_id);
        if game_id == nil or server_id == nil then return end

        web_bottom_control.excute_game_act(GameActions.kStartGame, 
            tonumber(game_id), tonumber(server_id));
    end;
    -- 安装游戏
    install_game = function(game_id_param)
        if game_id_param == nil then return end
        local game_id = tonumber(game_id_param);
        if game_id == 0 or banner_model.GAME_CENTER_GAME_ID == game_id then
            return
        end
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            game_lib:install_game(game_id);
        end
    end;
    -- 更新游戏 
    update_game = function(game_id_param, server_id_param)
        if game_id_param == nil then return end
        local game_id = tonumber(game_id_param);
        if game_id == 0 or banner_model.GAME_CENTER_GAME_ID == game_id then
            return
        end

        local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = game_id});
        if data_out ~= nil and not data_out.bAllowContinue then
            return
        end

        local server_id = 0;
        if server_id_param ~= nil then
            server_id = tonumber(server_id_param);
        end
        local action_info = { game_id_ = game_id, zone_id_ = server_id, version_ = "", uid_ = 0 };
        local json_str = base_utility.table_to_json(action_info);
        local server = banner_model.get_server_by_zoneid(game_id, server_id);
        if server ~= nil and server.pre_update == true then
            event_center.send_event(popup_wnd_event.PRE_UPDATE_GAME, json_str);
        else
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            game_lib:update_game(game_id, server_id, ierd_tgp.game_library.LAUNCH_OP_FROM_LANDING_PAGE);
        end;
    end;
    -- 更新插件
    update_plugin = function(game_id_param)
        if game_id_param == nil then return end
        local game_id = tonumber(game_id_param);
        if game_id == 0 or banner_model.GAME_CENTER_GAME_ID == game_id then
            return
        end

        local plugin_mgr =
            base_utility.get_component_adapter("IPlugin_mgr",
                "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
        if plugin_mgr ~= nil then
            plugin_mgr:UpdatePlugin(game_id);
        end
    end;

    check_update = function(json_str)
        local table_param = base_utility.json_to_table(json_str);
        if table_param ~= nil and table_param.game_id ~= nil 
            and table_param.callback_func ~= nil then
            local module_id = tonumber(table_param.game_id);
            local tbl_data = web_bottom_control.get_game_and_plugin_state_tbl(module_id);
            if true == web_bottom_control.table_is_empty(tbl_data) then return end
            local game_and_plugin_json = base_utility.table_to_json(tbl_data);
            web_utility.lua_call_js_ex(table_param.callback_func, game_and_plugin_json, CID_MAINFRAME_QBWEB);
        end
    end;

    get_game_and_plugin_state_tbl = function(module_id)
        local game_and_plugin_state = {};
        if module_id == nil then return game_and_plugin_state end

        local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil == game_lib then
            return game_and_plugin_state;
        end

        if module_id ~= 0 and banner_model.GAME_CENTER_GAME_ID ~= module_id then
            local game_state = banner_model.get_game_state(module_id);
            local plugin_state = banner_model.get_plugin_state(module_id);
            if game_state == nil and plugin_state == nil then
                return game_and_plugin_state;
            end
            game_and_plugin_state.retCode = 0;
            game_and_plugin_state.msg = "";
            game_and_plugin_state.gameid = module_id;
            if game_state ~= nil then
                game_and_plugin_state.game = {
                    game_id = module_id, status = game_state.iState, mid = game_state.game_id,
                    sub_status = game_state.iSub_state, progress = game_state.iProgress, 
                    size = game_state.iFile_size, left_time = game_state.iRemain_time,
                    priority = web_bottom_control.get_game_state_priority(game_state.iState, module_id),
                    speed = game_state.iDownload_speed, 
                    update_post_url = game_state.update_post_url,
                    silent_install = game_state.support_silent_install,
                    retCode = game_state.tcls_update_err_code, msg = game_state.szError_msg,
                    iIs_assistant_uin = game_state.iIs_assistant_uin,
                    iSupport_update = game_state.iSupport_update,
                    iAuto_update = game_state.iAuto_update,
                    iIs_hidden = game_state.iIs_hidden,
                    iSupport_preload = game_state.iSupport_preload,
                    iIs_preload = game_state.iIs_preload,
                    iPre_download = game_state.iPre_download,
                    iPre_download_dot = game_state.iPre_download_dot,
                    formal_update_after = game_state.formal_update_after,
                    last_adjust_progress_time = game_state.last_adjust_progress_time,
                    cancel_flag = game_state.cancel_flag,
                    is_show_new = game_state.is_show_new,
                    pre_download_status = game_state.pre_download_status,
                };
                local channel = game_lib:get_game_distribution_channels(module_id);
                if banner_model.is_netbar == 1 and 
                    banner_model.distribution_channels.CHANNELS_RAIL ~= channel and
                    game_state.iState == ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED then
                    -- 网吧版本未安装游戏只显示“开始游戏”，点击后提示联系网管
                    game_and_plugin_state.game.status = 0;
                end
                -- 优先显示OSS匹配到的版本公告配置
                local update_post_url_cfg = nil;
                local post_data = banner_model.game_version_post_data[game_and_plugin_state.game.game_id];
                local game_update_target_ver = game_state.update_target_ver;
                update_post_url_cfg = 
                    banner_model.handle_oss_version_post(
                        post_data, game_update_target_ver, game_and_plugin_state.game.game_id);
                if update_post_url_cfg ~= nil and update_post_url_cfg ~= "" then 
                    game_and_plugin_state.game.update_post_url = update_post_url_cfg;
                end

            end
            if plugin_state ~= nil then
                game_and_plugin_state.plugin = {
                retCode = 0, msg = "", game_id = module_id, mid = plugin_state.game_id,
                status = plugin_state.status, progress = plugin_state.progress,
                priority = banner_model.get_pluginstate_priority(plugin_state.status),  
            };
            end  
        end
        return game_and_plugin_state;
    end;

    open_game_dir = function(json_str)
        local lua_table = base_utility.json_to_table(json_str);
        if lua_table == nil or lua_table.game_id == nil then return end
        local game_id = tonumber(lua_table.game_id);
        if game_id == 0 or banner_model.GAME_CENTER_GAME_ID == game_id then
            return
        end
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
             game_lib:open_game_path(game_id);
        end
    end;

    handle_js_game_action = function(func_name, json_str)
        local table_param = base_utility.json_to_table(json_str);
        if table_param ~= nil and table_param.game_id ~= nil then
            local game_id = tonumber(table_param.game_id);
            if game_id == 0 or banner_model.GAME_CENTER_GAME_ID == game_id then
                return
            end
           local game_action = web_bottom_control.get_game_action_by_func(func_name);
           if game_action ~= nil and game_action ~= GameActions.kUnkown then
               web_bottom_control.excute_game_act(game_action, game_id);
           end
        end
    end;
    get_game_action_by_func = function(func_name)
        local game_action = GameActions.kUnkown;
        if 'jc_cancel_update_game' == func_name then
            game_action = GameActions.kCancelUpdate;
        elseif 'jc_cancel_update_plugin' == func_name then
            game_action = GameActions.kCancelUpdatePlugin;
        elseif 'jc_cancel_download' == func_name then
            game_action = GameActions.kCancelDownload;
        elseif 'jc_stop_game' == func_name then
            game_action = GameActions.kStopGame;
        elseif 'jc_continue_download_game' == func_name then
            game_action = GameActions.kContinueDownload;
        elseif 'jc_continue_update_game' == func_name then
            game_action = GameActions.kContinueUpdate;
        elseif 'jc_continue_update_plugin' == func_name then
            game_action = GameActions.kContinueUpdatePlugin;
        elseif 'jc_download_game' == func_name then
            game_action = GameActions.kDownloadGame;
        elseif 'jc_pause_download_game' == func_name then
            game_action = GameActions.kPauseDownload;
        elseif 'jc_pause_update_game' == func_name then
            game_action = GameActions.kPauseUpdate;
        elseif 'jc_pause_update_plugin' == func_name then
            game_action = GameActions.kPauseUpdatePlugin;
        end
        return game_action;
    end;

    excute_game_act = function(game_action, game_id, server_id)
        base_utility.log(string.format("excute_game_act, action:%d, game_id, %d",
            game_action, game_id));
        local zone_id = server_id;
        if zone_id == nil then
            zone_id = 0;
        end
        local action_info = {
                  game_id_ = game_id,
                  zone_id_ = zone_id,
                  version_ = 0,
                  uid_ = web_bottom_model.uin,
                  launch_op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_LANDING_PAGE
              };
        WGGameAction.excute_action(game_action, action_info);
    end; 

    table_is_empty = function(tbl)
        return _G.next(tbl) == nil;
    end;

    open_post = function(json_str)
        local lua_table = base_utility.json_to_table(json_str);
        if lua_table == nil or lua_table.game_id == nil then return end
        local game_id = tonumber(lua_table.game_id);
        if game_id == 0 or banner_model.GAME_CENTER_GAME_ID == game_id then
            return
        end
        local gl_state = banner_model.get_game_state(game_id);
        if gl_state == nil then return end
        local post_url = gl_state.update_post_url;

        -- 优先显示OSS匹配到的版本公告配置
        local update_post_url_cfg = nil;
        local post_data = banner_model.game_version_post_data[game_id];
        local game_update_target_ver = gl_state.update_target_ver;
        update_post_url_cfg = 
            banner_model.handle_oss_version_post(
                post_data, game_update_target_ver, game_id);
        if update_post_url_cfg ~= nil and update_post_url_cfg ~= "" then 
            post_url = update_post_url_cfg;
        end

        if post_url ~= nil then
            web_utility.open_link(post_url);
        end
    end;

    send_bottom_bar_show_state_to_js = function(json_str)
        if frame_bottom_control == nil then
            return;
        end;
        local table_param = base_utility.json_to_table(json_str);
        if table_param ~= nil and table_param.game_id ~= nil then
            local game_id = tonumber(table_param.game_id);
            -- 通知页面底栏显示状态
            local visible = 0;
            if frame_bottom_control.is_bottom_bar_show() == TRUE then
                visible = 1;
            else
                visible = 0;
            end
            frame_bottom_control.send_bottom_bar_state_to_js(game_id, visible);
        end
    end;
};

web_bottom_control.init();
